package org.gl.training.gwt.server.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.jms.ConnectionFactory;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

import org.gl.training.gwt.client.service.UserService;
import org.gl.training.gwt.server.utils.UserVOUtils;
import org.gl.training.gwt.shared.model.UserVO;
import org.gl.training.planner.dao.UserDao;
import org.gl.training.planner.model.Constants;
import org.gl.training.planner.model.MailMessage;
import org.gl.training.planner.model.User;
import org.gl.training.planner.model.UserHistory;
import org.gl.training.planner.model.UserTypes;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class UserServiceImpl extends RemoteServiceServlet implements
		UserService {

	private static final long serialVersionUID = 3616647336203202410L;

	@EJB
	private UserDao userDao;

	@Resource(mappedName = "jms/EmailConnectionFactory")
	private ConnectionFactory connectionFactory;

	@Resource(mappedName = "jms/EmailQueue")
	private Queue emailQueue;

	@Override
	public UserVO getUser(String userName) {
		User user = userDao.getUser(userName);
		return UserVOUtils.getUserVO(user);
	}

	@Override
	public List<UserVO> getUsers() {
		List<User> userList = userDao.getUsers();
		List<UserVO> userVOList = UserVOUtils.getUserVOs(userList);
		return userVOList;
	}

	@Override
	public void saveUser(UserVO userVO) {
		User user = UserVOUtils.getUser(userVO);

		List<UserTypes> userTypes = new ArrayList<UserTypes>();
		userTypes.add(UserTypes.USER);
		user.setUserTypesList(userTypes);

		user.setUserStatus(Constants.USER_STATUS_WAITING);

		List<UserHistory> userHistoryList = new ArrayList<UserHistory>();
		UserHistory userHistory = new UserHistory();
		userHistory.setTime(new Date());
		userHistory.setAction("User just registered");
		userHistory.setUser(user);
		user.setUserHistoryList(userHistoryList);
		userHistoryList.add(userHistory);

		userDao.save(user);
	}

	@Override
	public void delete(List<Integer> idList) {
		for (Integer id : idList) {
			userDao.delete(id);
		}
	}

	@Override
	public void approve(List<Integer> idList) {
		for (Integer id : idList) {
			User user = userDao.find(id);
			if (!user.getUserStatus().equals(Constants.USER_STATUS_APPROVED)) {
				user.setUserStatus(Constants.USER_STATUS_APPROVED);

				List<UserHistory> userHistoryList = user.getUserHistoryList();
				UserHistory userHistory = new UserHistory();
				userHistory.setTime(new Date());
				userHistory.setAction("User was approved");
				userHistory.setUser(user);
				user.setUserHistoryList(userHistoryList);
				userHistoryList.add(userHistory);

				userDao.save(user);
			}
		}
	}

	@Override
	public void sendEmail(List<Integer> idList) {
		for (Integer id : idList) {
			User user = userDao.find(id);

			MailMessage message = new MailMessage();
			message.setSubject("Confirmation");
			message.setBody("Congratulations, you are registered");
			message.setTo(user.getEmail());

			sendEmailToJms(message);
		}
	}

	private void sendEmailToJms(MailMessage mailMessage) {
		try {
			QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) connectionFactory;
			QueueConnection queueConnection = queueConnectionFactory
					.createQueueConnection();
			QueueSession session = queueConnection.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			QueueSender sender = session.createSender(emailQueue);
			ObjectMessage msg = session.createObjectMessage(mailMessage);
			sender.send(msg);
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}
}
